#include<iostream>
#include<cerrno>
#include<cstring>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include"comm.hpp" 
using namespace std;
int main() 
{
    umask(0);//常见管道文件，我们只需要一次创建
    int n=mkfifo(fifoname.c_str(),mode);
    if(n!=0)
    {
        std::cout<<errno<<":"<<strerror(errno)<<std::endl;
        return 1;
    }
    std::cout<<"create fifo file sucess"<<std::endl;
    //2.让服务端直接开枪管道文件
    int rfd=open(fifoname.c_str(),O_RDONLY);
    if(rfd<0)
    {
        std::cout<<errno<<":"<<strerror(errno)<<std::endl;
        return 2;
    }
    std::cout<<"open fifo sucess,begin ipc"<<std::endl;
    //3.正常通信
    char buffer[NUM];
    while(true)
    {
        buffer[0]=0;
        ssize_t n=read(rfd,buffer,sizeof(buffer)-1);
        if(n>0)
        {
            buffer[n]=0;
            std::cout<<"client:#"<<buffer<<std::endl;
        }
        else if(n==0)
        {
            std::cout<<"cilent quit,me too"<<std::endl;
            break;
        }
        else
        {
            std::cout<<":"<<strerror(errno)<<std::endl;
            break;
        }
    }
    close(rfd);

    return 0;
}